<template>
  <div class="template2">
    <el-container>
      <div class="head" id="head1">
        <div class="tit">订单统一分配
        </div>
        <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
          <el-form-item prop="house">
            <el-autocomplete size="mini" style="width:180px;" :maxlength='10' class="inline-input" v-model="formInline.house" :fetch-suggestions="querySearch" placeholder="小区名称" :trigger-on-focus="false" @select="handleSelect" format='yyyy-MM-dd'></el-autocomplete>
          </el-form-item>
          <el-form-item prop="region">
            <el-date-picker size='mini' clearable v-model="formInline.region" type="daterange" style="width:205px" range-separator="-"  start-placeholder="开始日期"  end-placeholder="结束日期"  value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
            <span @click="resetForm('formInline')" class="reset">
              <i class="icon iconfont icon-icon901"></i> 重置
            </span>
          </el-form-item>
        </el-form>
      </div>
      <el-main style="height:100%">
        <orderAllList></orderAllList>
      </el-main>
      <el-footer id="foot">
        <el-tooltip class="item" effect="dark" size='mini' content="分配管家后，发送服务时间通知业主" placement="bottom-start">
          <el-button plain style="float:left" size='small' @click="sendSMS">发短信</el-button>
        </el-tooltip>
      </el-footer>
    </el-container>
    <setServerTime></setServerTime>
    <setTime1></setTime1>
  </div>
</template>
<script> 
import orderAllList from './orderAllList.vue'  //订单列表
import setServerTime from '../serverOrder/setServerTime.vue' //分配管家
import setTime1 from '../serverOrder/setTime.vue'   //更改收房时间
import { request } from "../../assets/request.js";    
import { setTime } from "../../assets/session.js";
export default {
  data() {
    return {
      height: 0,
      formInline: {
        house: "",
        region: "",
      },
      restaurants: [],
      item:{},
      smsList:[],
    };
  },
  created() {
    this.loadAll()
    this.$root.$on("propertBank",data=>{
      this.listNum=data
    })
    this.$root.$on('selectSmsList',data=>{
      this.smsList=data
      console.log(data)
    })
  },
  methods: {
    resetForm(formName) {
      this.item={}
      this.$refs[formName].resetFields();
    },
    onSubmit() {   //搜索查询
      let formInline=this.formInline
      let item=this.item
      // console.log(formInline)
      if(!item.id){
        this.$message({
          message: "小区名称必选",
          type: "warning"
        });
        return
      }
      let obj ={
        housesId:item.id,
      }
      if(formInline.region[0] && formInline.region[1]){
        obj.startDate=formInline.region[0]
        obj.endDate=formInline.region[1]
      }
      // console.log(obj)
      this.$root.$emit('selOrder',obj)
    },
    pedestalList(){
      this.$root.$emit("pedestalList",'no')
    },
    selectSmsList(){
      
    },
    sendSMS(){//发送短信
      let smsList=this.smsList
      let that=this
      if(!smsList[0]){
        this.$message({
            type: "success",
            message: "请先选择业主！"
        });
        return;
      }
      let ids=''
      for(let i in smsList){
        ids=smsList[i].id+'-'
      }
      ids=ids.substr(0,ids.length-1);
      this.$confirm('此操作将给选中业主发送短信！', "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
        })
        .then(() => {
            let url = "order/sms?orderIds="+ids;
            let that=this   //解决this指向问题
            request.ajaxGet({
              url,
              that,
              fn(res){
                console.log(res)
                that.onSubmit()
                that.$message({
                    type: "success",
                    message: "操作成功"
                });
              }
            })
        })
        .catch(() => {
            this.$message({
                type: "info",
                message: "已取消操作"
            });
        });
    },
    loadAll() {
      let url = "houses/all";
      let that=this   //解决this指向问题
      request.ajaxGet({
        url,
        that,
        fn(res){
          for(let i in res){
            res[i].value=res[i].name
          }
          that.restaurants=res
          that.$store.state.property=res
        }
      })
    },
    querySearch(queryString, cb) {
      var restaurants = this.restaurants;
      var len = restaurants.length;
      var arr = [];
      for(var i=0;i<len;i++){
          //如果字符串中不包含目标字符会返回-1
          if(restaurants[i].value.indexOf(queryString)>=0){
              arr.push(restaurants[i]);
          }
      }
      cb(arr);
    },
    handleSelect(item) {
      console.log(item)
      this.item=item
    }
  },
  mounted() {

  },
  components:{
    orderAllList,   //查询订单列表
    setServerTime, //分配管家
    setTime1,     //设置时间
  },
};
</script>
<style scoped lang="less">
@import "../template2.less";
</style>